package com.servlet;

import java.io.IOException;
import java.io.PrintWriter;
import java.util.List;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;

import com.dao.AdminDao;
import com.pojo.AdminPojo;
import com.util.JsonUtil;
import com.util.Log;

public class LoadAdmin extends HttpServlet {

	public void doGet(HttpServletRequest request, HttpServletResponse response)
			throws ServletException, IOException{
		
		HttpSession session = request.getSession(true);
		
		String pageNum = request.getParameter("pageNum");
		String pageSize = request.getParameter("pageSize");
		String areaId = request.getParameter("areaId");
		String name =  request.getParameter("name");
		
		//当前页  
        int intPage = Integer.parseInt((pageNum == null || pageNum == "0") ? "1":pageNum);  
        //每页显示条数  
        int number = Integer.parseInt((pageSize == null || pageSize == "0") ? "10":pageSize);
        
		AdminPojo admin = new AdminPojo();
		AdminDao dao = new AdminDao();

		
		String sWhere = " where name!='"+session.getAttribute("curradmin").toString()+"' and name!='admincase' ";
		if(!"".equals(name)) sWhere += " and name like '%"+name+"%'";
		if(!"".equals(areaId))  sWhere += " and areaId ="+Integer.parseInt(areaId);
		List list = dao.findByPageApp(intPage, number, sWhere);
		String sList = "{";
        sList += "\"total\":"+dao.tabCount+", \"rows\":";	//VoteCount
        sList += JsonUtil.ListToJSON(list);
        sList += "}";
        dao.closeAll();
		Log.write(sList);
		
        response.setContentType("text/plain");
		response.setCharacterEncoding("UTF-8");
		PrintWriter out = response.getWriter();
		
		out.println(sList);
		out.flush();
        out.close();
	}

	public void doPost(HttpServletRequest request, HttpServletResponse response)
			throws ServletException, IOException {
		doGet(request, response);
	}

}
